package com.supor.aiot.community.ui.community;

import android.content.Intent;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.SystemUtils;
import com.android.baseconfig.constact.AppGlobalHost;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceFragment;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.ui.view.webkit.WebKitHelper;
import com.supor.aiot.common.utils.NotNullUtils;
import com.supor.aiot.common.utils.MPLoggerUtils;
import com.supor.aiot.community.databinding.FragmentCommunityBinding;

import androidx.annotation.Nullable;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * @author: zook
 * @date: 2021/10/28
 */
@Route(path = PathUrl.PATH_FRAGMENT_COMMUNITY)
public class CommunityFragment extends BaseMVVMEnhanceFragment<CommunityViewModel, FragmentCommunityBinding> {

    private WebKitHelper webKitHelper;

    public static CommunityFragment newInstance() {
        CommunityFragment fragment = new CommunityFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getArguments()) {
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void loadData() {
        super.loadData();
//        int statusBarHeight = SystemUtils.getDefaultStatusHeight(getContext());
//        bindingView.flContainer.setPadding(bindingView.flContainer.getPaddingLeft(),
//                statusBarHeight,
//                bindingView.flContainer.getPaddingRight(),
//                bindingView.flContainer.getPaddingBottom());
//        if (AppAgent.isLogin()) {
//            String token = BaseSharedPreferences.getString(getActivity(), HttpConstant.TOKEN);
//            if (!TextUtils.isEmpty(token)) {
//                webKitHelper.sendLoginToWeb(1, token);
//            }
//        }
    }

    @Override
    protected void initData() {
        super.initData();
        webKitHelper = new WebKitHelper();
        webKitHelper.embedWebKit(getActivity(), bindingView.flContainer);
//        webKitHelper.loadUrl(Constants.COMMUNITY_BASE);
        String city = "";
        if (null != GlobalDataCache.locationInfo) {
            city = NotNullUtils.getNotNullValue(GlobalDataCache.locationInfo.getCity());
        }
        webKitHelper.loadUrl(AppGlobalHost.getCommHost()
//                + "?globalAddress=" + city
//                + "&globalHeight=" + SystemUtils.getDefaultStatusHeight(activity)
                        + "?globalHeight=" + SystemUtils.getDefaultStatusHeight(activity)

        );


        RxBus3.getInstance().register(Constants.Tag.LOGIN, (Consumer<String>) s -> {
            if (Constants.Value.SUCCESS.equalsIgnoreCase(s)) {
                webKitHelper.sendLoginToWeb(1, BaseSharedPreferences.getString(getActivity(), HttpConstant.TOKEN));
            }
        }, this.getClass());
        RxBus3.getInstance().register(Constants.Tag.LOGOUT, (Consumer<String>) s -> {
            if (Constants.Value.SUCCESS.equalsIgnoreCase(s)) {
                webKitHelper.sendLoginToWeb(0, "");
            }
        }, this.getClass());

    }

    @Override
    public void onResume() {
        super.onResume();
        MPLoggerUtils.homeCommunityPageCount();
        if (null != webKitHelper) {
            webKitHelper.sendPageStateToWeb(0);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != webKitHelper) {
            webKitHelper.sendPageStateToWeb(1);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != webKitHelper) {
            webKitHelper.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != webKitHelper) {
            webKitHelper.destoryKit();
        }
    }
}